package com.yx.front.converter;

import cn.hutool.core.date.DateUtil;
import com.yx.front.model.ChapterMessage;
import com.yx.front.model.vo.BookChapterVo;
import com.yx.front.model.vo.ChapterMessageVo;
import com.yx.front.utils.TimeFormat;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YangXiong
 */
public class ChapterMessageConverter {
    public static List<BookChapterVo> handlerPage(List<ChapterMessage> records) {
        List<BookChapterVo> result = new ArrayList<>();
        records.forEach(one->{
            BookChapterVo chapterVo = getBookChapterVo(one);
            result.add(chapterVo);
        });
        return result;
    }

    public static BookChapterVo getBookChapterVo(ChapterMessage one) {
        BookChapterVo vo = new BookChapterVo();
        vo.setId(one.getId());
        vo.setChapterName(one.getChapterName());
        return vo;
    }

    public static ChapterMessageVo getChapterMessageVo(ChapterMessage chapterMessage) {
        ChapterMessageVo vo = new ChapterMessageVo();
        vo.setId(chapterMessage.getId());
        String res = getString(chapterMessage);
        vo.setChapterContext(res);
        vo.setBookId(chapterMessage.getBookId());
        vo.setChapterName(chapterMessage.getChapterName());
        String updateTime = DateUtil.format(chapterMessage.getUpdateTime(), TimeFormat.TIME_FORMAT);
        vo.setUpdateTime(updateTime);
        return vo;
    }

    private static String getString(ChapterMessage chapterMessage) {
        String context = chapterMessage.getChapterContext();
        String a = context.replace("。“", "。” <br>   &nbsp;");
        String b = a.replace("。", "。<br>   &nbsp;");
        return b.replace(";", ";<br>   &nbsp;");
    }
}
